package com.xpgk.mvc.ddc.zcdj.ywsl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.ddc.ddc_commomUtil.DDC_CommonUtilService;
import com.xpgk.mvc.ddc.ddc_commomUtil.PictureService;
import com.xpgk.mvc.ddc.entity.Business;
import com.xpgk.mvc.ddc.entity.Credentials;
import com.xpgk.mvc.ddc.entity.Owner;
import com.xpgk.mvc.ddc.entity.Plate;
import com.xpgk.mvc.ddc.entity.Vehicle;
import com.xpgk.mvc.ddc.entity.VehicleParemeters;
import com.xpgk.mvc.ddc.statistic.DailyStasticService;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.user.User;
import com.xpgk.util.SerialNumberUtil;


public class ZcdjYwslController  extends  BaseController{
	private DailyStasticService dailyStasticService;
	private PictureService pictureService = new PictureService();
	private DDC_CommonUtilService commonUtilService = new DDC_CommonUtilService();
	
	public  void  index(){
		Organization  org =  Organization.dao.findById(getCUserOrganIds());
		setAttr("LSH",SerialNumberUtil.getSerialNumber(org, "01"));
		setAttr("organids", getCUserOrganIds());
		render("ywsl.html");
	}
	
	public  void  getclxh(){
		List<Record> list=Db.find("select *  from ddc_vehicle_information   where  dataStatus =  '1'");
		renderJson(list);
	}
	
	public  void  getclxx(){
		String clxx=getPara("clxx");
		List<Record> list=Db.find("select *  from ddc_vehicle_information   where  model =  ? ",clxx);
		Record r = null;
		if(list!=null&&list.size()>0){
			 r = list.get(0);
		 }
		renderJson(r);
	}
	
	
	@Before(Tx.class)
	@SuppressWarnings("static-access")
	public  void  saveInfo(){	
		RenderBean result = new RenderBean();
		
		try {
			User user = getCUser();
			String jbrids = user.getStr(User.column_ids);
			String jbr = user.getStr(User.column_name);
			String organids = user.getStr(User.column_organizationids);
			Organization organization = Organization.dao.findById(organids);
			String organ = organization.getStr(Organization.column_name);
			
			SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			Business  business = getModel(Business.class,"business");
			VehicleParemeters  vehicleparemeters = getModel(VehicleParemeters.class,"vehicleparemeters");
			Owner  owner = getModel(Owner.class,"owner");
			Plate  plate = getModel(Plate.class,"plate");
			plate.set(Plate.column_HPHM, "晋B"+plate.get("HPHM"));
			List<Vehicle> lv = commonUtilService.getVehicleByCLSBDH(vehicleparemeters.get("CLSBDH").toString());
			List<Plate> lp = commonUtilService.getPlateByHPHM(plate.get("HPHM").toString());
			if(lv.size()>0){
				result.setCode(ConstantRender.render_error_code);
				result.setMsg("车辆识别代号已存在！");
			}else if(lp.size()>0){
				result.setCode(ConstantRender.render_error_code);
				result.setMsg("号牌号码已存在！");
			}else{
				Vehicle vehicle = new Vehicle();
				vehicle.set(vehicle.column_CLSBDH, vehicleparemeters.get("CLSBDH"));
				vehicle.set(vehicle.column_CLLX, vehicleparemeters.get("CLLX"));
				vehicle.set(vehicle.column_CCRQ, vehicleparemeters.get("CCRQ"));
				vehicle.set(vehicle.column_SYNX, vehicleparemeters.get("SYNX"));
				vehicle.set(vehicle.column_HPHM, plate.get("HPHM"));
				vehicle.set(vehicle.column_OWNERNAME, owner.get("OWNERNAME"));
				vehicle.set(vehicle.column_OWNERTYPE, owner.getStr(Owner.column_OWNERTYPE));
				
//				vehicle.set(vehicle.column_COMPANYNAME, owner.get("COMPANYNAME"));
				vehicle.set(vehicle.column_REGISTDATE, sdf.format(new Date()));
				vehicle.set(vehicle.column_ORGANIDS,organids);
				vehicle.set(vehicle.column_ORGAN,organ);
				vehicle.set(vehicle.column_JBRIDS,jbrids);
				vehicle.set(vehicle.column_JBR,jbr);
				vehicle.set(vehicle.column_STATUS,"1");
				vehicle.set(vehicle.column_BUSINESSTYPE,"1");
				vehicle.save();
				
				owner.set(owner.column_VEHICLEIDS, vehicle.get("IDS"));
				owner.set(owner.column_CREATEDATE, sdf.format(new Date()));
				owner.set(Owner.column_JBRIDS, jbrids);
				owner.save();
			
				vehicleparemeters.set(vehicleparemeters.column_VEHICLEIDS, vehicle.get("IDS"));
				vehicleparemeters.set(vehicleparemeters.column_CCRQ, vehicleparemeters.get("CCRQ"));
				vehicleparemeters.set(vehicleparemeters.column_HPHM, plate.get("HPHM"));
				vehicleparemeters.save();
				
				plate.set(plate.column_VEHICLEIDS, vehicle.get("IDS"));
				plate.set(plate.column_STATUS, "1");
				plate.set(plate.column_CREATEDATE, sdf.format(new Date()));
				plate.set(plate.column_JBRIDS, jbrids);
				plate.set(plate.column_ORGANIDS, organids);
				plate.save();
				
				Credentials credentials = new Credentials();
				credentials.set(credentials.column_HPHM, plate.get("HPHM"));
				credentials.set(credentials.column_DABH, business.get("LSH"));
				credentials.set(credentials.column_VEHICLEIDS, vehicle.get("IDS"));
				credentials.set(credentials.column_CLLX, vehicleparemeters.get("CLLX"));
				credentials.set(credentials.column_OWNERNAME, owner.get("OWNERNAME"));
				credentials.set(credentials.column_ADDRESS, owner.get("ADDRESS"));
				credentials.set(credentials.column_DETAILADDRESS, owner.get("DETAILADDRESS"));
				credentials.set(credentials.column_SYXZ, vehicleparemeters.get("SYXZ"));
				credentials.set(credentials.column_CLPP, vehicleparemeters.get("CLPP"));
				credentials.set(credentials.column_CLXH, vehicleparemeters.get("CLXH"));
				credentials.set(credentials.column_ZCZL, vehicleparemeters.get("ZCZL"));
				credentials.set(credentials.column_HDZRS, vehicleparemeters.get("HDZRS"));
				credentials.set(credentials.column_STATUS,"1");
				credentials.set(credentials.column_CREATEDATE, sdf.format(new Date()));
				credentials.set(credentials.column_ORGANIDS,organids);
				credentials.set(credentials.column_ORGAN,organ);
				credentials.set(credentials.column_JBRIDS,jbrids);
				credentials.set(credentials.column_JBR,jbr);
				credentials.save();
				
				business.set(business.column_TYPE, "1");
				business.set(business.column_VEHICLEIDS, vehicle.get("IDS"));
				business.set(business.column_CLSBDH, vehicleparemeters.get("CLSBDH"));
				business.set(business.column_NEWCREDENTIALSIDS, credentials.get("IDS"));
				business.set(business.column_STATUS, "1");
				business.set(business.column_CLLX, vehicleparemeters.get("CLLX"));
				business.set(business.column_NEWOWNERIDS, owner.get("IDS"));
				business.set(business.column_NEWOWNERNAME, owner.get("OWNERNAME"));
//				business.set(business.column_NEWCOMPANYNAME, owner.get("COMPANYNAME"));
				business.set(business.column_NEWPAREMETERSIDS, vehicleparemeters.get("IDS"));
				business.set(business.column_CLPP, vehicleparemeters.get("CLPP"));
				business.set(business.column_NEWPLATEIDS, plate.get("IDS"));
				business.set(business.column_NEWHPHM, plate.get("HPHM"));
				business.set(business.column_ORGANIDS, organids);
				business.set(business.column_ORGANNAME, organ);
				business.set(business.column_YWSLDATE, sdf.format(new Date()));
				business.set(business.column_YWSLJBRIDS, jbrids);
				business.set(business.column_YWSLJBR, jbr);
				business.set(business.column_NEWOWNERTYPE, owner.getStr(Owner.column_OWNERTYPE));
				business.set(business.column_NEWCARDNUMBER, owner.getStr(Owner.column_SFZHM));
				business.save();
				
				Owner.dao.findById(owner.get("IDS")).set(Owner.column_BUSINESSIDS, business.get("IDS")).update();
				Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_PLATEIDS, plate.get("IDS")).update();
				Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_OWNERIDS, owner.get("IDS")).update();
				Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_CREDENTIALSIDS, credentials.get("IDS")).update();
				Vehicle.dao.findById(vehicle.get("IDS")).set(Vehicle.column_VEHICLEPAREMETERSIDS, vehicleparemeters.get("IDS")).update();
				VehicleParemeters.dao.findById(vehicleparemeters.get("IDS")).set(VehicleParemeters.column_PLATEIDS, plate.get("IDS")).update();
				
				dailyStasticService.saveDailyStastic(organids,organ,sdf.format(new Date()),"1","1");
				
				String picStr1 = getPara("picStr1");
				String picStr2 = getPara("picStr2");
				String picStr3 = getPara("picStr3");
				String picStr4 = getPara("picStr4");
				String BUSINESSIDS = business.get("IDS");
				String VEHICLEIDS = vehicle.get("IDS");
				if(picStr1.length()>0){
					pictureService.uploadPhoto(picStr1,BUSINESSIDS,VEHICLEIDS,"PICTURE1","PICTURE1");
				}
				if(picStr2.length()>0){
					pictureService.uploadPhoto(picStr2,BUSINESSIDS,VEHICLEIDS,"PICTURE2","PICTURE2");
				}
				if(picStr3.length()>0){
					pictureService.uploadPhoto(picStr3,BUSINESSIDS,VEHICLEIDS,"PICTURE3","PICTURE3");
				}
				if(picStr4.length()>0){
					pictureService.uploadPhoto(picStr4,BUSINESSIDS,VEHICLEIDS,"PICTURE4","PICTURE4");
				}
				result.setCode(ConstantRender.render_success_code);
				result.setMsg("保存成功!");
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result.setCode(ConstantRender.render_error_code);
			result.setMsg("保存失败");
		}
		renderJson(result);
	}
	
}
